
<template>
	<view class="msj_biji">
		<view class="goback" @click="goback">
			
		</view>
		
		<view class="msj_biji_content">
			<textarea v-model="value" @input='onchange' style="width: 100%; font-size: 14px;" placeholder="#你好2023#"></textarea>
			<view class="msj_biji_zhuti">
			<view style="font-size: 14px; color: #666;" class="">
				添加主题标签
			</view>
			<view class="msj_biji_biao">
				<view  v-for="(item,index) in biaoq" @click="addbiaoq(item,index)" :key="index"
						:class="item.check?'msj_biji_qian_a':'msj_biji_qian'" >
					<view @click.stop='dele(index)' class="msj_circle">
						<u-icon   name="close-circle-fill" color="#bbb"
						size="15"></u-icon>
					</view>
					<text>{{item.name}}</text>
				</view>
			</view>
		</view>
		</view>
		
		<view class="msj_biji_btn">
			<u-button style="width: 100px; border-radius: 25px;" size='small' type="warning" @click='fabu'>发布</u-button>
		</view>
	</view>
</template>

<script>
	import {getmsjusermy,updatemsjusermy,
		addzwysqtj} from '../../../utils/api.js'
	export default {
		data() {
			return {
				value:'',
				xuanz: [],
				content:'是否存入草稿?',
				user:{},
				biaoq: [{
						name: '#早餐',
						check: false,
					},
					{
						name: '#午餐',
						check: false,
					},
					{
						name: '#晚餐',
						check: false,
					},
					{
						name: '#下午茶',
						check: false,
					}
				]
			}
		},
		onShow() {
			let token = JSON.parse(uni.getStorageSync('token'))
			getmsjusermy(token.userid).then(res => {
				let data = res.data
				data.zuopin = JSON.parse(data.zuopin)
				data.caogao=JSON.parse(data.caogao)
				this.user = data
				console.log(this.user);
			})
		},
		onLoad() {
	
		},
		methods: {	
			// 返回 
			goback(){
				if(this.value){
					uni.showModal({
						content: this.content,
						cancelText:'否',
						confirmText:'是',
						success: (res)=> {
							if (res.confirm) {
								let obj={
									val:this.value,
									imgurl:'[]',
									xuanz: JSON.stringify(this.xuanz),
								}
								obj.id=this.user.caogao.length>0?this.user.caogao[0].id+1:1

								this.user.caogao.unshift(obj)
								this.user.zuopin=JSON.stringify(this.user.zuopin)
								this.user.caogao=JSON.stringify(this.user.caogao)
								updatemsjusermy(this.user).then(res1=>{
									uni.showToast({
										icon: 'success',
										title: '已存入草稿',
										duration: 2000
									});
									uni.navigateBack()
								})
							} else if (res.cancel) {
								uni.navigateBack()
							}
						}
					});
				}else{
					uni.navigateBack()
				}
			},
			fabu(){
				if(this.value){
					let times=new Date().toLocaleString().replaceAll('/','-')
					let obj={
						img:this.user.author,
						name:this.user.username,
						user:`${this.user.id}`,
						like:'0',
						zhuan:'0',
						time:times,
						pl:'[]',
						image:'[]',
						title:this.xuanz.join('')+this.value
					}
					let obj2={
						title:obj.title,
						time:times,
						image:obj.image,
						userid:obj.user
					}
					this.user.zuopin.unshift(obj2)
					this.user.zuopin=JSON.stringify(this.user.zuopin)
					this.user.caogao=JSON.stringify(this.user.caogao)
					// console.log(obj);
					updatemsjusermy(this.user).then(res=>{
						addzwysqtj(obj).then(res2=>{
							if(res.code==200&&res2.code==200){
							uni.showToast({
								icon: 'success',
								title: '发布成功',
								duration: 2000
							});
								uni.switchTab({
									url:'/pages/tabbar/tabbar-2/tabbar-2'
								})
							}
						})
					})
				}else{
					uni.showToast({
						icon: 'error',
						title: '请填写完整',
						duration: 2000
					});
				}
			},
			onchange(e){
				this.value = e.detail.value.replace(/\s+/g, '')
			},
			dele(i) {
				let v=this.biaoq.splice(i, 1)
				let flag = this.xuanz.indexOf(v[0].name)
				if (flag > -1) {
					this.xuanz.splice(flag, 1)
				}
			},
			addbiaoq(v, i) {
				v.check = !v.check
				this.biaoq.forEach(item => {
					if (item.check) {
						let flag = this.xuanz.indexOf(item.name)
						if (flag > -1) {
							this.xuanz.splice(flag, 1)
						}
						this.xuanz.unshift(item.name)
					} else {
						let flag = this.xuanz.indexOf(item.name)
						if (flag > -1) {
							this.xuanz.splice(flag, 1)
						}
					}
				})
			}
		}
	};
</script>

<style>
	.goback{
		position: fixed;
		top: 0;left: 0;
		    width: 28px;
		    height: 42px; z-index: 999;
	}
	.msj_biji{
		width: 100%;
		height:  calc(100vh - 44px);
		background: #F4F4F4;
		box-sizing: border-box;
		padding: 15px 0 0;
	}
	.msj_biji_content{
		width: 100%;
		background: #fff;
		box-sizing: border-box;
		padding: 10px;
	}
	
	.msj_biji_zhuti{
		width: 100%;
		box-sizing: border-box;
		padding: 10px 0;
	}
	.msj_biji_biao{
		width: 100%;
		display: flex;
		box-sizing: border-box;
		padding: 10px 5px;
	}
	.msj_biji_qian_a,.msj_biji_qian{
		font-size: 13px;
		color: #666; position: relative;
		background: #F4F5F9;
		padding: 5px 20px; margin-right: 10px;
		border-radius: 25px;
	}
	.msj_biji_qian_a{
		background: #256FE9;
		color: #fff;
	}
	.msj_circle{
		position: absolute;
		top: -5px;right: 3px;
	}
	.msj_biji_btn{
		width: 100%;
		display: flex; justify-content: center;
		padding: 20px 0 0;
	}
</style>
